using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ANDDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procAND_add]";
        private string _strSPUpdateName = "dbo.[procAND_update]";
        private string _strSPDeleteName = "dbo.[procAND_delete]";
        private string _strSPGetName = "dbo.[procAND_get]";
        private string _strSPGetCategoryName = "dbo.[sp_procAND_getcategory]";
        private string _strSPGetAllName = "dbo.[procAND_getall]";
        private string _strSPGetPages = "dbo.[procAND_getpaged]";
        private string _strSPIsExist = "dbo.[procAND_isexist]";
        private string _strSPGetTransferOutName = "dbo.[procAND_gettransferout]";
        private string _strTableName = "AND";
        #endregion Local Variable

        #region Method
        public ANDInfo Get(
            String SUN_DB,
            String CATEGORY,
            String SUBCATEGORY
        , ref string sErr)
        {
            ANDInfo objEntr = new ANDInfo();
            connect();
            InitSPCommand(_strSPGetName);
            AddParameter("SUN_DB", SUN_DB);
            AddParameter("CATEGORY", CATEGORY);
            AddParameter("SUBCATEGORY", SUBCATEGORY);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();

            if (list.Rows.Count > 0)
                objEntr = (ANDInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            ANDInfo result = new ANDInfo();
            result.SUN_DB = (dt.Rows[i]["SUN_DB"] == DBNull.Value ? "" : (String)dt.Rows[i]["SUN_DB"]);
            result.LOOKUP = (dt.Rows[i]["LOOKUP"] == DBNull.Value ? "" : (String)dt.Rows[i]["LOOKUP"]);
            result.UPDATED = (dt.Rows[i]["UPDATED"] == DBNull.Value ? "" : (String)dt.Rows[i]["UPDATED"]);
            result.CATEGORY = (dt.Rows[i]["CATEGORY"] == DBNull.Value ? "" : (String)dt.Rows[i]["CATEGORY"]);
            result.SUBCATEGORY = (dt.Rows[i]["SUBCATEGORY"] == DBNull.Value ? "" : (String)dt.Rows[i]["SUBCATEGORY"]);
            result.DESCRIPTION = (dt.Rows[i]["DESCRIPTION"] == DBNull.Value ? "" : (String)dt.Rows[i]["DESCRIPTION"]);
            result.SHORTDESC = (dt.Rows[i]["SHORTDESC"] == DBNull.Value ? "" : (String)dt.Rows[i]["SHORTDESC"]);
            result.MASK = (dt.Rows[i]["MASK"] == DBNull.Value ? "" : (String)dt.Rows[i]["MASK"]);
            result.NONVALIDATED = (dt.Rows[i]["NONVALIDATED"] == DBNull.Value ? "" : (String)dt.Rows[i]["NONVALIDATED"]);
            result.AMENDABLE = (dt.Rows[i]["AMENDABLE"] == DBNull.Value ? "" : (String)dt.Rows[i]["AMENDABLE"]);

            return result;
        }

        public DataTable GetAll(
            String SUN_DB
        , ref string sErr)
        {
            DataTable dtList = null;
            connect();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = _strSPGetAllName;
            AddParameter("SUN_DB", SUN_DB);

            try
            {
                dtList = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return dtList;
        }
        public DataTable GetCategory_AND(
            String SUN_DB
        , String CATEGORY, ref string sErr)
        {
            DataTable dtList = null;
            connect();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = _strSPGetCategoryName;
            AddParameter("SUN_DB", SUN_DB);
            AddParameter("CATEGORY", CATEGORY);

            try
            {
                dtList = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return dtList;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(ANDInfo objEntr, ref string sErr)
        {
            int ret = -1;
            connect();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = _strSPInsertName;
            AddParameter("SUN_DB", objEntr.SUN_DB);
            AddParameter("LOOKUP", objEntr.LOOKUP);
            AddParameter("UPDATED", objEntr.UPDATED);
            AddParameter("CATEGORY", objEntr.CATEGORY);
            AddParameter("SUBCATEGORY", objEntr.SUBCATEGORY);
            AddParameter("DESCRIPTION", objEntr.DESCRIPTION);
            AddParameter("SHORTDESC", objEntr.SHORTDESC);
            AddParameter("MASK", objEntr.MASK);
            AddParameter("NONVALIDATED", objEntr.NONVALIDATED);
            AddParameter("AMENDABLE", objEntr.AMENDABLE);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = command.ExecuteScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(ANDInfo objEntr)
        {

            connect();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = _strSPUpdateName;
            AddParameter("SUN_DB", objEntr.SUN_DB);
            AddParameter("LOOKUP", objEntr.LOOKUP);
            AddParameter("UPDATED", objEntr.UPDATED);
            AddParameter("CATEGORY", objEntr.CATEGORY);
            AddParameter("SUBCATEGORY", objEntr.SUBCATEGORY);
            AddParameter("DESCRIPTION", objEntr.DESCRIPTION);
            AddParameter("SHORTDESC", objEntr.SHORTDESC);
            AddParameter("MASK", objEntr.MASK);
            AddParameter("NONVALIDATED", objEntr.NONVALIDATED);
            AddParameter("AMENDABLE", objEntr.AMENDABLE);

            string sErr = "";
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
            String SUN_DB,
            String CATEGORY,
            String SUBCATEGORY
        )
        {
            connect();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = _strSPDeleteName;
            AddParameter("SUN_DB", SUN_DB);
            AddParameter("CATEGORY", CATEGORY);
            AddParameter("SUBCATEGORY", SUBCATEGORY);

            string sErr = "";
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }
        /*
       public DataTableCollection Get_Page(ANDInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
       {
           string whereClause = CreateWhereClause(obj);
           DataTableCollection dtList = null;
           connect();
           command.Connection = connection;
           command.CommandType = CommandType.StoredProcedure;
           command.CommandText = _strSPGetPages;
           AddParameter("WhereClause", whereClause);
           AddParameter("OrderBy", orderBy);
           AddParameter("PageIndex", pageIndex);
           AddParameter("PageSize", pageSize);           
            
           try
           {               
               dtList = executeCollectSelectSP(command);
           }
           catch (Exception ex)
           {
               sErr = ex.Message;
           }
           disconnect();
           if (sErr != "") ErrorLog.SetLog(sErr);
           return dtList;
       }
       */

        public Boolean IsExist(
            String SUN_DB,
            String CATEGORY,
            String SUBCATEGORY
        )
        {
            connect();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = _strSPIsExist;
            AddParameter("SUN_DB", SUN_DB);
            AddParameter("CATEGORY", CATEGORY);
            AddParameter("SUBCATEGORY", SUBCATEGORY);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(ANDInfo obj)
        {
            String result = "";

            return result;
        }


        public DataTable Search(string columnName, string columnValue, string condition, String tableName, ref string sErr)
        {
            string query = "select * from " + tableName + " where columnName " + condition + " " + columnValue;
            DataTable list = new DataTable();
            connect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method


        public DataTable GetTransferOut(string dtb, string from, string to, ref string sErr)
        {
            DataTable dtList = null;
            connect();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = _strSPGetTransferOutName;
            AddParameter("SUN_DB", dtb);
            AddParameter("CATEGORY_FROM", from);
            AddParameter("CATEGORY_TO", to);

            try
            {
                dtList = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return dtList;
        }
    }
}
